/*
 * Copyright 2009 Kindleit Technologies. All rights reserved. This file, all
 * proprietary knowledge and algorithms it details are the sole property of
 * Kindleit Technologies unless otherwise specified. The software this file
 * belong with is the confidential and proprietary information of Kindleit
 * Technologies. ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with Kindleit.
 */


package net.kindleit.mpd.jukebox;

import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.bff.javampd.MPD;
import org.bff.javampd.MPDPlayer;
import org.bff.javampd.exception.MPDConnectionException;
import org.bff.javampd.exception.MPDPlayerException;

/** MPDControlPanel is responsible of
 *
 * @author rhansen@kindleit.net
 */
public class MPDControlPanel extends Panel {

  public static final class ChangeVolumeButton extends AjaxLink<Void> {
    private static final long serialVersionUID = 1L;

    private final MPD mpd;

    private final int delta;

    private ChangeVolumeButton(final String id, final MPD mpd, final int delta) {
      super(id);
      this.mpd = mpd;
      this.delta = delta;
    }

    @Override
    public void onClick(final AjaxRequestTarget target) {
      final MPDPlayer player = mpd.getMPDPlayer();
      try {
        player.setVolume(player.getVolume() + delta);
      } catch (final MPDPlayerException e) {
        throw new WicketRuntimeException(e);
      } catch (final MPDConnectionException e) {
        throw new WicketRuntimeException(e);
      }
      target.addComponent(getParent());
    }
  }

  /** */
  private static final long serialVersionUID = 1L;
  private final MPD mpd;

  public MPDControlPanel(final String id, final MPD mpd) {
    super(id);
    this.mpd = mpd;
    setOutputMarkupId(true);
    setDefaultModel(new CompoundPropertyModel<MPD>(mpd));
    add(new Label("MPDPlayer.volume"));
    add(new ChangeVolumeButton("raiseVolume", mpd, 2));
    add(new ChangeVolumeButton("lowerVolume", mpd, -2));
  }

}

